﻿using NHibernate;
using NHibernate.Cfg;
using System.IO;

namespace JSC.Data.NHibernate
{
    public static class NHibernateSession
    {
        public static void Init(string cfgFile)
        {
            Configuration cfg = ConfigureNHibernate(cfgFile);

            SessionFactory = cfg.BuildSessionFactory();
        }

        private static Configuration ConfigureNHibernate(string cfgFile)
        {
            Configuration cfg = new Configuration();

            if (string.IsNullOrEmpty(cfgFile))
                return cfg.Configure();
            else
                return cfg.Configure(cfgFile);
        }

        public static void Init(DirectoryInfo directoryInfo)
        {
            Configuration cfg = new Configuration()
                .AddDirectory(directoryInfo).Configure();
            
            SessionFactory = cfg.BuildSessionFactory();
        }

        public static ISessionFactory SessionFactory { get; set; }

        public static ISession Current
        {
            get
            {
                return SessionFactory.GetCurrentSession();
            }
        }
    }
}
